package com.itpeng.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itpeng.dao.MemberDao;
import com.itpeng.dao.OrderDao;
import com.itpeng.dao.OrderSettingDao;
import com.itpeng.exception.HealthException;
import com.itpeng.pojo.Member;
import com.itpeng.pojo.Order;
import com.itpeng.pojo.OrderSetting;
import com.itpeng.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Aythor:彭彦钦
 * @Data: 2020/9/25  20:57
 */
@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 提交预约
     * @param orderInfo
     * @return
     */
    @Override
    @Transactional
    public Order submitOrder(Map<String, String> orderInfo) {
        //1. 根据日期查询当天是否可以预约
        //  得到Date类型的日期
        String orderDateStr = orderInfo.get("orderDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date orderDate = null;
        try {
            orderDate = sdf.parse(orderDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new HealthException("日期格式错误");
        }
        //  进行查询，得到预约设置
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        //  若所选的日期无预约设置，则报异常
        if (orderSetting == null){
            throw new HealthException("您所选的日期，无法进行预约");
        }
        //  若所选日期的预约人数已满，则报异常
        if (orderSetting.getReservations() >= orderSetting.getNumber()){
            throw new HealthException("预约人数已满，请选择其他日期");
        }
        //若都没问题，则继续执行代码
        //2. 根据手机号判断预约人是否是会员
        //  先获得用户输入的手机号
        String telephone = orderInfo.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        //3. 若不会空，则表明该用户是会员
        //  创建订单对象，放入数据
        Order order = new Order();
        order.setOrderDate(orderDate);//放入预约日期
        order.setSetmealId(Integer.parseInt(orderInfo.get("setmealId")));//放入预约的套餐
        //  判断是否存在该会员
        if (member == null){
            //若为空，则表明不是会员，则将其添加成会员
            member = new Member();
            member.setName(orderInfo.get("name"));
            member.setSex(orderInfo.get("sex"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            member.setPassword("123456");
            member.setIdCard(orderInfo.get("idCard"));
            member.setRemark("预约注册用户");
            //添加到会员表中
            memberDao.add(member);
        }else {
            //若不为空，表名是会员
            order.setMemberId(member.getId());//该订单的预约用户
            // 若是会员，则查询订单是否重复
            List<Order> orderList = orderDao.findByCondition(order);
            //若满足以下条件，则代表订单重复，则报错
            if (orderList != null && orderList.size() > 0){
                throw new HealthException("订单不能重复预约");
            }
        }
        //4. 若订单无重复，或已经添加为会员，则添加订单到表中
        order.setMemberId(member.getId());//该订单的预约用户
        order.setOrderStatus(Order.ORDERSTATUS_NO);//放入订单状态
        order.setOrderType(orderInfo.get("orderType"));//放入订单类型
        orderDao.add(order);

        //5. 成功添加订单后，则在预约设置中为已预约人数+1
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return order;
    }

    /**
     * 根据ID得到预约信息
     * @param id
     * @return
     */
    @Override
    public Map<String, String> findById(Integer id) {
        return orderSettingDao.findById(id);
    }
}
